package com.brotherly.framework.net;

import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

import com.brotherly.framework.http.HttpListener;
import com.brotherly.framework.http.HttpService;
import com.brotherly.framework.service.ServiceManager;

public class NetHandler
{
    public static final String TAG = "NetHandler";
    public static final int CONNECT_TIMEOUT = 30000;
    public static final int SOCKET_TIMEOUT = 30000;

    protected HttpService httpServ = null;

    public static class NetHttpListener implements HttpListener
    {
        public int code;
        public Map<String, String> headers;
        public byte[] body;

        @Override
        public void result(int code, Map<String, String> headers, byte[] body)
        {
            this.code = code;
            this.headers = headers;
            this.body = body;
        }

        public String getContent()
        {
            if (body == null)
                return null;

            try
            {
                return new String(body, "utf-8");
            }
            catch (Exception e)
            {}

            return null;
        }
    }

    public NetHandler(HttpService httpServ)
    {
        this.httpServ = httpServ;
    }

    public NetResponse doHttpGet(String url, String str_headers)
    {
        NetResponse response = new NetResponse();

        JSONObject headers = null;

        NetHttpListener listener = new NetHttpListener();

        try
        {
            headers = (JSONObject) (new JSONParser()).parse(str_headers);
        }
        catch (Exception e)
        {}

        try
        {
            if (httpServ == null)
            {
                httpServ = (HttpService) ServiceManager.getInstance().getService(ServiceManager.HTTP_SERVICE);
            }

            httpServ.request(url, "get", headers, null, listener, true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        response.code = listener.code;
        response.headers = listener.headers;
        response.resultBytes = listener.body;
        response.result = listener.getContent();

        return response;
    }

    public NetResponse doHttpPost(String url, String str_headers, String request)
    {
        NetResponse response = new NetResponse();

        JSONObject headers = null;

        NetHttpListener listener = new NetHttpListener();

        try
        {
            headers = (JSONObject) (new JSONParser()).parse(str_headers);
        }
        catch (Exception e)
        {}

        try
        {
            if (httpServ == null)
            {
                httpServ = (HttpService) ServiceManager.getInstance().getService(ServiceManager.HTTP_SERVICE);
            }

            httpServ.request(url, "post", headers, request.getBytes("utf-8"), listener, true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        response.code = listener.code;
        response.headers = listener.headers;
        response.resultBytes = listener.body;
        response.result = listener.getContent();

        return response;
    }
}
